package liming.ai.code.v3.data;

import org.json.JSONObject;

import java.io.File;

public class FileInfo {
    // 标准目录配置
    public static final String SOURCE_DIR = "src/main/java";
    public static final String RESOURCE_DIR = "src/main/resources";
    public static final String CLASS_DIR = "target/classes";
    public static final String DOCS_DIR = "target/generated-docs";

    private final String projectPath;// 项目根目录
    private final String packageName;// 包名
    private final String className;// 类名
    private final File javaFile;// java文件
    private final File classFile;// class文件
    private final File mdFile;// md文件
    private Exception exception;// 异常信息

    public FileInfo(String projectPath, String packageName, String className) {
        this.projectPath = projectPath;
        this.packageName = packageName;
        this.className = className;

        // 构建相对路径
        String packagePath = packageName.replace(".", File.separator);
        String basePath = packagePath + File.separator + className;

        // 使用标准目录构建文件路径
        this.javaFile = buildPath(SOURCE_DIR, basePath + ".java");
        this.classFile = buildPath(CLASS_DIR, basePath + ".class");
        this.mdFile = buildPath(DOCS_DIR, basePath + ".md");
    }

    private File buildPath(String baseDir, String relativePath) {
        return new File(new File(projectPath, baseDir), relativePath);
    }

    /**
     * 获取项目根目录
     */
    public String getJavaRootPath(){
        return projectPath + File.separator + SOURCE_DIR;
    }

    /**
     * 获取资源文件根目录
     */
    public String getResourceRootPath(){
        return projectPath + File.separator + RESOURCE_DIR;
    }

    public String getClassRootPath(){
        return projectPath + File.separator + CLASS_DIR;
    }

    public String getDocsRootPath(){
        return projectPath + File.separator + DOCS_DIR;
    }

    public String getFullClassName(){
        return packageName + "." + className;
    }

    public String getProjectPath() {
        return projectPath;
    }

    public String getPackageName() {
        return packageName;
    }

    public String getClassName() {
        return className;
    }

    public File getJavaFile() {
        return javaFile;
    }

    public File getClassFile() {
        return classFile;
    }

    public File getMdFile() {
        return mdFile;
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    public JSONObject toJson() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("projectPath", projectPath);
        jsonObject.put("packageName", packageName);
        jsonObject.put("className", className);
        jsonObject.put("javaFile", javaFile.getAbsolutePath());
        jsonObject.put("classFile", classFile.getAbsolutePath());
        jsonObject.put("mdFile", mdFile.getAbsolutePath());
        return jsonObject;
    }
}
